package org.bigstones.orgnzr.model.impl.jpa;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import org.bigstones.orgnzr.model.impl.jpa.entities.PlanTemplate;
import org.bigstones.orgnzr.model.impl.jpa.entities.StepTemplate;
import org.bigstones.orgnzr.model.impl.jpa.entities.User;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class SetupDao {

	public void registerInitialData() throws Exception{
		resetAllData();
		EntityManager em = null;
		try{
			em = EMF.get().createEntityManager();
			User brice = new User();
			brice.setFirstName("Brice");
			brice.setLastName("Copy");
			brice.setUserId("brice.copy@gmail.com");
			
			User cedric = new User();
			cedric.setFirstName("Cedric");
			cedric.setLastName("Copy");
			cedric.setUserId("cedric.copy@gmail.com");
			
			PlanTemplate pt = new PlanTemplate();
			pt.setTitle("Organize your wedding");
			pt.setDescription("Plan the most beautiful day of your life. Make sure you don't forget anything.");
			pt.setBudget(10000.0);
			pt.setDuration(365 * 24 * 3600 * 1000L);
			pt.setExpert(brice);
			
			StepTemplate st1 = new StepTemplate();
			st1.setTitle("Register at the town hall");
			st1.setPhaseTitle("Preparations");
			st1.setBudget(0.0);
			st1.setCountdownIndex(3 * 31 * 24 * 3600 * 1000L);
			st1.setIsMandatory(true);
						
			StepTemplate st2 = new StepTemplate();
			st2.setTitle("Secure a venue");
			st2.setPhaseTitle("Preparations");
			st2.setBudget(500.0);
			st2.setCountdownIndex(365 * 24 * 3600 * 1000L);
			st2.setIsMandatory(true);
			
			StepTemplate st3 = new StepTemplate();
			st3.setTitle("Invite guests");
			st3.setPhaseTitle("Preparations");
			st3.setBudget(200.0);
			st3.setCountdownIndex(150 * 24 * 3600 * 1000L);
			st3.setIsMandatory(false);
			
			StepTemplate st4 = new StepTemplate();
			st4.setTitle("Decorate the venue");
			st4.setPhaseTitle("The Big Day");
			st4.setBudget(300.0);
			st4.setCountdownIndex(1 * 24 * 3600 * 1000L);
			st4.setIsMandatory(false);
			
			if(pt.getStepTemplates() == null){
				pt.setStepTemplates(new ArrayList<StepTemplate>());
			}
			pt.getStepTemplates().add(st1);
			pt.getStepTemplates().add(st2);
			pt.getStepTemplates().add(st3);
			pt.getStepTemplates().add(st4);
			
			{
				EntityTransaction t = em.getTransaction();
				t.begin();
				em.persist(pt);
				t.commit();
				}

//			{
//				EntityTransaction t = em.getTransaction();
//				t.begin();
//				em.persist(brice);
//				t.commit();
//			}

			{
			EntityTransaction t = em.getTransaction();
			t.begin();
			em.persist(cedric);
			t.commit();
			}
			
			
		}catch(Throwable t){
			throw new Exception(t);
		}finally{
			if(em!= null) em.close();
		}		
	}
	
	public void resetAllData() throws Exception{
		EntityManager em = null;
		try{
			em = EMF.get().createEntityManager();
			
			Query qs = em.createQuery("Select s from StepTemplate s");	
			List<StepTemplate> steps = (List<StepTemplate>)qs.getResultList();
			Query qp = em.createQuery("Select p from PlanTemplate p");	
			List<PlanTemplate> plans = (List<PlanTemplate>)qp.getResultList();
			Query q = em.createQuery("Select u from User u");	
			List<User> users = (List<User>)q.getResultList();
			
			
			for (StepTemplate ent : steps) {
				em.remove(ent);
			}
			for (PlanTemplate ent : plans) {
				em.remove(ent);
			}
			for (User ent : users) {
//				EntityTransaction t = em.getTransaction();
//				t.begin();
				em.remove(ent);
			}
			
		}catch(Throwable t){
			throw new Exception(t);
		}finally{
			if(em!= null) em.close();
		}	
	}
}
